﻿using Kingdee.BOS;
using Kingdee.BOS.App.Data;
using Kingdee.BOS.Core.DynamicForm.PlugIn;
using Kingdee.BOS.Core.DynamicForm.PlugIn.Args;
using Kingdee.BOS.Orm.DataEntity;
using Kingdee.BOS.Util;
using System;
using System.ComponentModel;
using System.Data;
namespace Kingdee.Bos.Project.DyForm
{
    [Description("【服务插件】价格调整单反审核，修改价目表对应值")]
    [HotUpdate]

    public class JiaGeTiaoZhengFanShengHe : AbstractOperationServicePlugIn
    {


        public override void OnPreparePropertys(PreparePropertysEventArgs e)
        {
            base.OnPreparePropertys(e);
            // 添加预加载字段
            e.FieldKeys.Add("BillNo");
        }
        public override void EndOperationTransaction(EndOperationTransactionArgs e)
        {
            base.EndOperationTransaction(e);
            foreach (DynamicObject item in e.DataEntitys)
            {
                var FBillNo = item["BillNo"].ToString();//编号

                //修改【销售价目表】。价格=【价格调整单】。调整后价格
                //获取数据库相应数据
                string SQLselect = "select FBILLNO AS '单据编号',F_VHTP_WLMC AS '物料', " +
                       " F_VHTP_JM1 AS '价目表1',F_VHTP_DJ11 AS '价目表1调前含税单价',F_VHTP_DJ12 AS '价目表1调后含税单价', " +
                       " F_VHTP_JM2 AS '价目表2',F_VHTP_DJ21 AS '价目表2调前含税单价',F_VHTP_DJ22 AS '价目表2调后含税单价', " +
                       " F_VHTP_JM3 AS '价目表3',F_VHTP_DJ31 AS '价目表3调前含税单价',F_VHTP_DJ32 AS '价目表3调后含税单价', " +
                       " F_VHTP_JM4 AS '价目表4',F_VHTP_DJ41 AS '价目表4调前含税单价',F_VHTP_DJ42 AS '价目表4调后含税单价', " +
                       " F_VHTP_JM5 AS '价目表5',F_VHTP_DJ51 AS '价目表5调前含税单价',F_VHTP_DJ52 AS '价目表5调后含税单价', " +
                       " F_VHTP_JM6 AS '价目表6',F_VHTP_DJ61 AS '价目表6调前含税单价',F_VHTP_DJ62 AS '价目表6调后含税单价', " +
                       " F_VHTP_JM7 AS '价目表7',F_VHTP_DJ71 AS '价目表7调前含税单价',F_VHTP_DJ72 AS '价目表7调后含税单价', " +
                       " F_VHTP_JM8 AS '价目表8',F_VHTP_DJ81 AS '价目表8调前含税单价',F_VHTP_DJ82 AS '价目表8调后含税单价', " +
                       " F_VHTP_JM9 AS '价目表9',F_VHTP_DJ91 AS '价目表9调前含税单价',F_VHTP_DJ92 AS '价目表9调后含税单价', " +
                       " F_VHTP_JM10 AS '价目表10',F_VHTP_DJ101 AS '价目表10调前含税单价',F_VHTP_DJ102 AS '价目表10调后含税单价' " +
                       " FROM VHTP_t_Cust100006 T1 " +
                       " LEFT JOIN VHTP_t_Cust_Entry100002 T2 " +
                       " ON T2.FID = T1.FID " +
                       " where FBILLNO = '" + FBillNo + "' ";
                DataTable dt1;
                dt1 = DBUtils.ExecuteDataSet(this.Context, SQLselect).Tables[0];

                //数据库获取数据判断是否存在重复值
                string SQLselect2 = "SELECT COUNT(F_VHTP_WLMC),F_VHTP_WLMC " +
                    "from VHTP_t_Cust100006 T1 LEFT JOIN VHTP_t_Cust_Entry100002 T2 ON T2.FID=T1.FID  " +
                    "where FBILLNO= '" + FBillNo + "' group by F_VHTP_WLMC";
                DataTable dt2;
                dt2 = DBUtils.ExecuteDataSet(this.Context, SQLselect2).Tables[0];

                //循环dt2 判断是否存在重复物料 若存在 改变PuanDuanZhi
                int countdt2 = dt2.Rows.Count;
                string PuanDuanZhi = "true";

                for (int a = 0; a < countdt2; a++)
                {
                    double count = double.Parse(dt2.Rows[a][0].ToString());
                    if (count > 1)
                    {
                        PuanDuanZhi = "false";
                    }
                }
                //PuanDuanZhi
                //不存在重复物料
                if (PuanDuanZhi == "true")
                {

                    if (dt1.Rows.Count > 0)
                    {
                        for (int i = 0; i < dt1.Rows.Count; i++)
                        {
                            //获取各字段值
                            //共32个字段
                            String FBILLNO = dt1.Rows[i][0].ToString();
                            string FMATERIALID = dt1.Rows[i][1].ToString();

                            //修改各个价目表数据
                            //价目表1
                            string JMID1 = dt1.Rows[i][2].ToString();
                            string oldFPRICE1 = dt1.Rows[i][3].ToString();
                            string newFPRICE1 = dt1.Rows[i][4].ToString();

                            if (JMID1 != "0")
                            {
                                string update1 = "update T_SAL_PRICELISTENTRY set FPRICE = '" + oldFPRICE1 + "',FDOWNPRICE='" + oldFPRICE1 + "'     " +
                               "WHERE FID = '" + JMID1 + "' AND FMATERIALID= '" + FMATERIALID + "'";
                                DBUtils.ExecuteDynamicObject(this.Context, update1);
                            }

                            //价目表2
                            string JMID2 = dt1.Rows[i][5].ToString();
                            string oldFPRICE2 = dt1.Rows[i][6].ToString();
                            string newFPRICE2 = dt1.Rows[i][7].ToString();

                            if (JMID2 != "0")
                            {
                                string update2 = "update T_SAL_PRICELISTENTRY set FPRICE = '" + oldFPRICE2 + "',FDOWNPRICE='" + oldFPRICE2 + "'     " +
                               "WHERE FID = '" + JMID2 + "' AND FMATERIALID= '" + FMATERIALID + "'";
                                DBUtils.ExecuteDynamicObject(this.Context, update2);
                            }

                            //价目表3
                            string JMID3 = dt1.Rows[i][8].ToString();
                            string oldFPRICE3 = dt1.Rows[i][9].ToString();
                            string newFPRICE3 = dt1.Rows[i][10].ToString();

                            if (JMID3 != "0")
                            {
                                string update3 = "update T_SAL_PRICELISTENTRY set FPRICE = '" + oldFPRICE3 + "',FDOWNPRICE='" + oldFPRICE3 + "'     " +
                               "WHERE FID = '" + JMID3 + "' AND FMATERIALID= '" + FMATERIALID + "'";
                                DBUtils.ExecuteDynamicObject(this.Context, update3);
                            }

                            //价目表4
                            string JMID4 = dt1.Rows[i][11].ToString();
                            string oldFPRICE4 = dt1.Rows[i][12].ToString();
                            string newFPRICE4 = dt1.Rows[i][13].ToString();

                            if (JMID4 != "0")
                            {
                                string update4 = "update T_SAL_PRICELISTENTRY set FPRICE = '" + oldFPRICE4 + "',FDOWNPRICE='" + oldFPRICE4 + "'     " +
                               "WHERE FID = '" + JMID4 + "' AND FMATERIALID= '" + FMATERIALID + "'";
                                DBUtils.ExecuteDynamicObject(this.Context, update4);
                            }

                            //价目表5
                            string JMID5 = dt1.Rows[i][14].ToString();
                            string oldFPRICE5 = dt1.Rows[i][15].ToString();
                            string newFPRICE5 = dt1.Rows[i][16].ToString();

                            if (JMID5 != "0")
                            {
                                string update5 = "update T_SAL_PRICELISTENTRY set FPRICE = '" + oldFPRICE5 + "',FDOWNPRICE='" + oldFPRICE5 + "'     " +
                               "WHERE FID = '" + JMID5 + "' AND FMATERIALID= '" + FMATERIALID + "'";
                                DBUtils.ExecuteDynamicObject(this.Context, update5);
                            }

                            //价目表6
                            string JMID6 = dt1.Rows[i][17].ToString();
                            string oldFPRICE6 = dt1.Rows[i][18].ToString();
                            string newFPRICE6 = dt1.Rows[i][19].ToString();

                            if (JMID6 != "0")
                            {
                                string update6 = "update T_SAL_PRICELISTENTRY set FPRICE = '" + oldFPRICE6 + "',FDOWNPRICE='" + oldFPRICE6 + "'     " +
                               "WHERE FID = '" + JMID6 + "' AND FMATERIALID= '" + FMATERIALID + "'";
                                DBUtils.ExecuteDynamicObject(this.Context, update6);
                            }

                            //价目表7
                            string JMID7 = dt1.Rows[i][20].ToString();
                            string oldFPRICE7 = dt1.Rows[i][21].ToString();
                            string newFPRICE7 = dt1.Rows[i][22].ToString();

                            if (JMID7 != "0")
                            {
                                string update7 = "update T_SAL_PRICELISTENTRY set FPRICE = '" + oldFPRICE7 + "',FDOWNPRICE='" + oldFPRICE7 + "'     " +
                               "WHERE FID = '" + JMID7 + "' AND FMATERIALID= '" + FMATERIALID + "'";
                                DBUtils.ExecuteDynamicObject(this.Context, update7);
                            }

                            //价目表8
                            string JMID8 = dt1.Rows[i][23].ToString();
                            string oldFPRICE8 = dt1.Rows[i][24].ToString();
                            string newFPRICE8 = dt1.Rows[i][25].ToString();

                            if (JMID8 != "0")
                            {
                                string update8 = "update T_SAL_PRICELISTENTRY set FPRICE = '" + oldFPRICE8 + "',FDOWNPRICE='" + oldFPRICE8 + "'     " +
                               "WHERE FID = '" + JMID8 + "' AND FMATERIALID= '" + FMATERIALID + "'";
                                DBUtils.ExecuteDynamicObject(this.Context, update8);
                            }

                            //价目表9
                            string JMID9 = dt1.Rows[i][26].ToString();
                            string oldFPRICE9 = dt1.Rows[i][27].ToString();
                            string newFPRICE9 = dt1.Rows[i][28].ToString();

                            if (JMID9 != "0")
                            {
                                string update9 = "update T_SAL_PRICELISTENTRY set FPRICE = '" + oldFPRICE9 + "',FDOWNPRICE='" + oldFPRICE9 + "'     " +
                               "WHERE FID = '" + JMID9 + "' AND FMATERIALID= '" + FMATERIALID + "'";
                                DBUtils.ExecuteDynamicObject(this.Context, update9);
                            }

                            //价目表10
                            string JMID10 = dt1.Rows[i][29].ToString();
                            string oldFPRICE10 = dt1.Rows[i][30].ToString();
                            string newFPRICE10 = dt1.Rows[i][31].ToString();

                            if (JMID10 != "0")
                            {
                                string update10 = "update T_SAL_PRICELISTENTRY set FPRICE = '" + oldFPRICE10 + "',FDOWNPRICE='" + oldFPRICE10 + "'     " +
                               "WHERE FID = '" + JMID10 + "' AND FMATERIALID= '" + FMATERIALID + "'";
                                DBUtils.ExecuteDynamicObject(this.Context, update10);
                            }


                        }
                    }
                }
                else
                {
                    //若存在重复值 警告
                    throw new KDBusinessException("", "存在重复物料！");
                }


            }
        }

    }

}

